package com.cn.ycy.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

@Controller
@RequestMapping("upload")
public class UploadFileController {

    /**
     * 上传
     */
    @PostMapping(path = "/uploadFile")
    @ResponseBody
    public String uploadFile(HttpServletRequest request, @RequestParam MultipartFile[] files) {
        StringBuilder content = new StringBuilder();
        for (MultipartFile file : files) {
            try {
                byte[] bytes = file.getBytes();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(file.getInputStream()));
                String line = null;
                while ((line = bufferedReader.readLine()) != null) {
                    content.append(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return content.toString();
    }

    /**
     * 获取图片流
     */
    @GetMapping(path = "/image")
    public void getImageIO(HttpServletResponse httpServletResponse) {
        InputStream in = null;
        OutputStream out = null;
        try {
            in = new FileInputStream("/Users/ycy/Desktop/linshi/h.png");
            out = httpServletResponse.getOutputStream();
            int legth = 0;
            byte[] buf = new byte[1024];
            while ((legth = in.read(buf)) != -1) {
                out.write(buf, 0, legth);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


}
